#! /usr/bin/python3
# coding=utf-8
# ******************************************************************************
# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
# licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Author: senlin
# Create: 2022-1-19
# ******************************************************************************/

import argparse

import os
from genericpath import isfile
from src.libs.base import save2csv
from src.api.obs import OpensuseObsAPI
from src.libs.logger import logger
from src.release.check_lang import IdentifyBUildDependLanguage

def analyse_packages_language(packages_spec_path, packages=None):
    """
    analyse languages of packages
    Args:
        packages_spec_path:
        packages:
    Returns:
    
    """
    res_csv_file_name = 'Packages_Languages.csv'
    csv_header = ['Package', 'Languages', 'Detail']
    packages_languages = []
    ana = IdentifyBUildDependLanguage(packages_spec_path)
    if not packages:
        specs = os.listdir(packages_spec_path)
        packages = [spec_file.replace('.spec', '') for spec_file in specs]

    for pkg in packages:
        build_requires, build_prase = ana.get_build_info(pkg)
        lamguages, detail = ana.analyse_languages(build_requires, build_prase)
        packages_languages.append([pkg, lamguages, detail])
   
    save2csv(res_csv_file_name, 'w', csv_header, packages_languages)
    

if __name__ == "__main__":
    # parser = argparse.ArgumentParser()
    # #parser.add_argument("pkg", type=str, default=None, help="The Package to be Queried")
    # parser.add_argument("-pl", "--package_list", default=None, nargs="?", 
    #                     help=" choose the package list form new feature")
    # args = parser.parse_args()
    # if not args.package_list:
    #     logger.error("args invalid, please use -h for help")
    #     quit()

    packages_spec_path = '/home/master-spec-file'
    pkgs = []
    with open('/home/oect/src/release/pkgs', encoding='utf-8') as f:
        for line in f.readlines():
            pkgs.append(line.strip())
            
    analyse_packages_language(packages_spec_path)
